package cn.com.xy.sms.sdk.number;

import java.util.Map;

import org.json.JSONArray;

import cn.com.xy.sms.sdk.Iservice.XyCallBack;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.entity.NumberInfoManager;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.service.number.UploadUserTagService;
import cn.com.xy.sms.sdk.util.BaseRunnable;
import cn.com.xy.sms.sdk.util.CallBackState;

/* SDK-428 huangzhiqiang 20160603 start */
public class UploadUserTagRunnable extends BaseRunnable {
    private static BaseRunnable mBaseRunnable = null;
    private static final int mLimit = 20;

	public static BaseRunnable getInstance() {
		synchronized (UploadUserTagRunnable.class) {
			if (mBaseRunnable == null) {
				mBaseRunnable = new UploadUserTagRunnable();
			}
		}
		
		return mBaseRunnable;
	}
	
	private UploadUserTagRunnable() {
		mExecuteInterval = Constant.HOUR;	    
	}
	
    @Override
    public void execute() {
        Map<String, String[]> needUploadUserTagInfoMap = NumberInfoManager.needUploadUserTagInfo(mLimit);
        uploadUserTagToService(needUploadUserTagInfoMap);
    }

    private static void uploadUserTagToService(Map<String, String[]> needUploadUserTagInfoMap) {
        UploadUserTagService.uploadToService(needUploadUserTagInfoMap, null, new XyCallBack() {

            @Override
            public void execute(Object... obj) {
                try {
                    int callBackState = (Integer) obj[0];
                    if (callBackState != CallBackState.UPLOAD_COMPLETE) {
                        return;
                    }

                    JSONArray uploadSuccessNums = (JSONArray) obj[2];
                    ParseNumberCache.updateUserTagUploadStatusCache(uploadSuccessNums);
                } catch (Throwable e) {
                    LogManager.e(Constant.TAG,
                            "UploadUserTagRunnable uploadUserTagToService execute error:", e);
                }
            }
        });
    }
}
/* SDK-428 huangzhiqiang 20160603 end */